//
//  LLPickerView.m
//  picker
//
//  Created by nonoking on 2017/7/17.
//  Copyright © 2017年 nonoking. All rights reserved.
//

#import "LLPickerView.h"

@interface LLPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UIPickerView *ffPV;

/**保存结果*/
@property(nonatomic,copy)NSString *resultStr;

@end

@implementation LLPickerView

+ (instancetype)createPickerWithDatasourse:(NSDictionary *)datasourse headerArray:(NSArray *)headerArray
{
    NSBundle *crrentBundle = [NSBundle bundleForClass:[self class]];
    LLPickerView *picker = [[crrentBundle loadNibNamed:@"LLPickerView" owner:nil options:nil]firstObject];
    picker.datasoursePV = [datasourse mutableCopy];
    picker.headerArrayPV = [headerArray mutableCopy];
    
    return picker;
}


+ (instancetype)createPicker
{
    return [self createPickerWithDatasourse:nil headerArray:nil];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        
    }
    return self;
}


- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setupUI];
}

#pragma mark - 设置ui
- (void)setupUI
{
    self.ffPV.delegate = self;
    self.ffPV.dataSource = self;
}

#pragma mark - 设置数据
- (void)vcLoadDatasourse
{
    
}

#pragma mark - 注册通知和实现
- (void)vcRegistNotific
{
    
}

#pragma mark - 懒加载

- (NSMutableDictionary *)datasoursePV
{
    if (_datasoursePV == nil) {
        _datasoursePV = [NSMutableDictionary dictionary];
    }
    return _datasoursePV;
}

- (NSMutableArray *)headerArrayPV
{
    if (_headerArrayPV == nil) {
        _headerArrayPV = [NSMutableArray array];
    }
    return _headerArrayPV;
}

#pragma mark - 点击事件
- (IBAction)clickAction:(UIButton *)sender
{
    NSMutableString *resultM = [NSMutableString string];
    for (NSUInteger idx = 0; idx < self.headerArrayPV.count; idx++) {
        NSString *title = self.headerArrayPV[idx];
        NSArray *datas = [self.datasoursePV objectForKey:title];
        if (self.headerArrayPV.count - 1 == idx) {
            [resultM appendFormat:@"%@",datas[[self.ffPV selectedRowInComponent:idx]]];
        }else{
            [resultM appendFormat:@"%@ - ",datas[[self.ffPV selectedRowInComponent:idx]]];
        }
        
    }
    if (self.Selectedhandle) {
        self.Selectedhandle([resultM copy]);
    }
    
}


#pragma mark - 业务逻辑

#pragma mark - 自定义代理


#pragma mark - picker代理
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.headerArrayPV.count;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSString *title = self.headerArrayPV[component];
    NSArray *datas = [self.datasoursePV objectForKey:title];
    return datas.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *title = self.headerArrayPV[component];
    NSArray *datas = [self.datasoursePV objectForKey:title];
    return datas[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *title = self.headerArrayPV[component];
    NSArray *datas = [self.datasoursePV objectForKey:title];
    self.resultStr = [NSString stringWithFormat:@"%@",datas[row]];
    NSLog(@"%@",self.resultStr);
}

@end
